package com.example.pacs.dicom.server.cstoresup;

import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;

import java.util.Date;

/**
 * dcm文件解析类
 */
public class DicomReader {

    /**
     * data set info
     */
    private final Attributes attr;

    /**
     * meta data info
     */
    private final Attributes fmi;

    public DicomReader(Attributes attr, Attributes fmi) {
        this.attr = attr;
        this.fmi = fmi;
    }

    /**
     * 获取患者的ID
     */
    public String getPatientId() {
        return this.attr.getString(Tag.PatientID);
    }

    /**
     * 获取患者的名称
     */
    public String getPatientName() {
        return this.attr.getString(Tag.PatientName);
    }

    /**
     * 获取患者的性别
     */
    public String getPatientSex() {
        return this.attr.getString(Tag.PatientSex);
    }

    /**
     * 获取患者的年龄
     */
    public String getPatientAge() {
        return this.attr.getString(Tag.PatientAge);
    }

    /**
     * 获取患者的生日
     */
    public Date getPatientBirthday() {
        return this.attr.getDate(Tag.PatientBirthDate);
    }

    /**
     * 获取登记号
     */
    public String getAccessionNumber() {
        return attr.getString(Tag.AccessionNumber);
    }

    /**
     * 获取其他患者病史
     */
    public String getAdditionalPatientHistory() {
        return this.attr.getString(Tag.AdditionalPatientHistory);
    }

    /**
     * 获取入院诊断说明
     */
    public String getAdmittingDiagnosesDescription() {
        return this.attr.getString(Tag.AdmittingDiagnosesDescription);
    }

    /**
     * 获取咨询医师
     */
    public String getReferringPhysicianName() {
        return this.attr.getString(Tag.ReferringPhysicianName);
    }

    /**
     * 获取患者检查项时间
     */
    public Date getStudyDateTime() {
        return this.attr.getDate(Tag.StudyDateAndTime);
    }

    /**
     * 获取studyId
     */
    public String getStudyID() {
        return this.attr.getString(Tag.StudyID);
    }

    /**
     * 获取检查项描述
     */
    public String getStudyDescription() {
        return this.attr.getString(Tag.StudyDescription);
    }

    /**
     * 获取instanceId
     */
    public String getStudyInstanceUID() {
        return this.attr.getString(Tag.StudyInstanceUID);
    }

    /**
     * 获取study优先级
     */
    public String getStudyPriorityID() {
        return this.attr.getString(Tag.StudyPriorityID);
    }

    /**
     * 获取检查项状态
     */
    public String getStudyStatusID() {
        return this.attr.getString(Tag.StudyStatusID);
    }

    public String getBodyPartExamined() { return this.attr.getString(Tag.BodyPartExamined); }
    public String getLaterality() { return this.attr.getString(Tag.Laterality); }
    public String getOperatorsName() { return this.attr.getString(Tag.OperatorsName); }
    public String getPatientPosition() { return this.attr.getString(Tag.PatientPosition); }
    public String getProtocolName() { return this.attr.getString(Tag.ProtocolName); }
    public Date getSeriesDateTime() { return  this.attr.getDate(Tag.SeriesDateAndTime); }
    public String getSeriesDescription(){ return this.attr.getString(Tag.SeriesDescription); }
    public String getSeriesInstanceUID() { return this.attr.getString(Tag.SeriesInstanceUID); }
    public Integer getSeriesNumber() { return this.attr.getInt(Tag.SeriesNumber, 0); }
    public String getConversionType() { return this.attr.getString(Tag.ConversionType); }
    public String getDeviceSerialNumber() { return this.attr.getString(Tag.DeviceSerialNumber); }
    public String getInstitutionAddress() { return this.attr.getString(Tag.InstitutionAddress); }
    public String getInstitutionName() { return this.attr.getString(Tag.InstitutionName); }
    public String getInstitutionalDepartmentName() { return this.attr.getString(Tag.InstitutionalDepartmentName);}
    public String getManufacturer() { return this.attr.getString(Tag.Manufacturer); }
    public String getManufacturerModelName() { return this.attr.getString(Tag.ManufacturerModelName); }
    public String getModality() { return this.attr.getString(Tag.Modality); }
    public String getSoftwareVersion(){ return this.attr.getString(Tag.SoftwareVersions); }
    public String getStationName() { return this.attr.getString(Tag.StationName); }
    public Date getAcquisitionDateTime() { return this.attr.getDate(Tag.AcquisitionDateAndTime); }
    public Date getContentDateTime() { return this.attr.getDate(Tag.ContentDateAndTime);}
    public Integer getExposureTime() { return this.attr.getInt(Tag.ExposureTime, 0); }
    public String getImageOrientation() { return this.attr.getString(Tag.ImageOrientation); }
    public String getImagePosition() { return this.attr.getString(Tag.ImagePosition); }
    public String getImageType() { return this.attr.getString(Tag.ImageType); }
    public Integer getInstanceNumber() { return this.attr.getInt(Tag.InstanceNumber, 0); }
    public String getKvp() { return this.attr.getString(Tag.KVP); }
    public String getMediaStorageSopInstanceUID() { return this.fmi.getString(Tag.MediaStorageSOPInstanceUID); }//InstanceUID -> it is also the file name itself
    public String getTransferSyntaxUID() { return this.fmi.getString(Tag.TransferSyntaxUID); }
    public String getPatientOrientation() { return this.attr.getString(Tag.PatientOrientation); }
    public Float getPixelSpacing() { return this.attr.getFloat(Tag.PixelSpacing, 0); }
    public Float getSliceLocation() { return this.attr.getFloat(Tag.SliceLocation, 0); }
    public Float getSliceThickness() { return this.attr.getFloat(Tag.SliceThickness, 0); }
    public String getSopClassUID() { return this.attr.getString(Tag.SOPClassUID); }
    public String getSOPInstanceUID() { return this.attr.getString(Tag.SOPInstanceUID); }
    public String getWindowCenter() { return this.attr.getString(Tag.WindowCenter); }
    public String getWindowWidth() { return this.attr.getString(Tag.WindowWidth); }
    public Integer getXrayTubeCurrent() { return this.attr.getInt(Tag.XRayTubeCurrent, 0); }

    @Override
    public String toString() {

        return String.format(
                "[Patient] => Patient ID: %s, Patient Name: %s, Patient Sex: %s, Patient Age: %s, Patient Birthday: %s \n" +
                        "----------------------------------------------------------------------------------------------------------------------\n"+
                        "[Study] => Accession Number: %s, Additional Patient History: %s, Admitting Diagnoses Description: %s, Referring Physician Name: %s, SOP Instance UID: %s, Study Date Time: %s, Study ID: %s, Study Instance UID: %s, Study Priority ID: %s, Study Status ID: %s \n"+
                        "----------------------------------------------------------------------------------------------------------------------\n"+
                        "[Series] => Body Part Examined: %s, Laterality: %s, Operators Name: %s, Patient Position: %s, Protocol Name: %s, Series Date Time: %s, Series Description: %s, Series Instance UID: %s, Series Number: %d \n"+
                        "----------------------------------------------------------------------------------------------------------------------\n"+
                        "[Equipment] => Conversion Type: %s, Institutional Department Name: %s, Device SerialNumber: %s, Instituition Address: %s, Institution Name: %s, Manufacturer: %s, Manufacturer Model Name: %s, Modality: %s, Software Version: %s, Station Name: %s\n"+
                        "----------------------------------------------------------------------------------------------------------------------\n"+
                        "[Instance] --> Acquisition Date Time: %s, Content Date Time: %s, Exposure Time: %s, Image Orientation: %s, Image Position: %s, Image Type: %s, Instance Number: %d, kvp: %s,  Media Storage SOP InstanceUID: %s, TransferSyntax UID: %s, Patient Orientation:  %s, Pixel Spacing: %f, Slice Location: %f, Slice Thickness: %f, SOP Class UID: %s, SOP Instance UID: %s, window Center: %s, window Width: %s, Xray Tube Current: %d \n",
                getPatientId(), getPatientName(), getPatientSex(), getPatientAge(), getPatientBirthday(),
                getAccessionNumber(), getAdditionalPatientHistory(), getAdmittingDiagnosesDescription(), getReferringPhysicianName(), getSOPInstanceUID(), getStudyDateTime(), getStudyID(), getStudyInstanceUID(), getStudyPriorityID(),getStudyStatusID(),
                getBodyPartExamined(), getLaterality(), getOperatorsName(), getPatientPosition(), getProtocolName(), getSeriesDateTime(), getSeriesDescription(), getSeriesInstanceUID(), getSeriesNumber(),
                getConversionType(),  getInstitutionalDepartmentName(), getDeviceSerialNumber(), getInstitutionAddress(), getInstitutionName(), getManufacturer(), getManufacturerModelName(), getModality(), getSoftwareVersion(), getStationName(),
                getAcquisitionDateTime(), getContentDateTime(), getExposureTime(), getImageOrientation(), getImagePosition(), getImageType(), getInstanceNumber(), getKvp(), getMediaStorageSopInstanceUID(), getTransferSyntaxUID(), getPatientOrientation(), getPixelSpacing(), getSliceLocation(), getSliceThickness(), getSopClassUID(), getSOPInstanceUID(), getWindowCenter(), getWindowWidth(), getXrayTubeCurrent()
        );
    }
}
